package org.pk.preferences;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.PlatformUI;

import com.cs.pk.Activator;
import com.cs.pk.utils.Constants;
import com.cs.pk.utils.Util;

public class NDSBuildPreferences extends PreferencePage implements
		IWorkbenchPreferencePage {

	private Text commandTxt, rootPath, enviornmentTxt, buildCmdTxt = null;
	//private IPreferenceStore store = null;
	private String rootDir, enviornmentVar, commandVar, buildCmdVar = null;

	public NDSBuildPreferences() {
	}

	public NDSBuildPreferences(String title) {
		super(title);
	}

	public NDSBuildPreferences(String title, ImageDescriptor image) {
		super(title, image);
	}

	protected Control createContents(Composite parent) {
		Control control = buildControl(parent);
		setValues();
		return control;
	}

	public void init(IWorkbench workbench) {
		setPreferenceStore(PlatformUI.getPreferenceStore());
		rootDir = Util.getValues(Constants.BUILD_ROOT_DIR);
		enviornmentVar = Util.getValues(Constants.BUILD_ENV_VARIABLE);
		commandVar = Util.getValues(Constants.BUILD_COMMAND);
		buildCmdVar = Util.getValues(Constants.CYGWIN_CMD);
	}


	protected IPreferenceStore doGetPreferenceStore() {
		return getPreferenceStore();
	}

	public boolean performOk() {
		getPreferenceStore().setValue(Constants.BUILD_ROOT_DIR,
				rootPath.getText().trim());
		getPreferenceStore().setValue(Constants.BUILD_ENV_VARIABLE,
				enviornmentTxt.getText().trim());
		getPreferenceStore().setValue(Constants.BUILD_COMMAND,
				commandTxt.getText().trim());
		getPreferenceStore().setValue(Constants.CYGWIN_CMD,
				buildCmdTxt.getText().trim());
		Activator.getDefault().savePluginPreferences();
		return super.performOk();
	}

	protected void performDefaults() {
		rootPath.setText(Activator.getDefault().getDefaultPreference(
				Constants.BUILD_ROOT_DIR));
		enviornmentTxt.setText(Activator.getDefault().getDefaultPreference(
				Constants.BUILD_ENV_VARIABLE));
		commandTxt.setText(Activator.getDefault().getDefaultPreference(
				Constants.BUILD_COMMAND));
		buildCmdTxt.setText(Activator.getDefault().getDefaultPreference(
				Constants.CYGWIN_CMD));
		getPreferenceStore().setToDefault(Constants.BUILD_COMMAND);
		getPreferenceStore().setToDefault(Constants.BUILD_ENV_VARIABLE);
		getPreferenceStore().setToDefault(Constants.BUILD_ROOT_DIR);
		getPreferenceStore().setToDefault(Constants.CYGWIN_CMD);
		super.performDefaults();
	}

	private Control buildControl(Composite parent) {

		Composite mainComposite = new Composite(parent, SWT.NULL| SWT.BORDER );

		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.grabExcessHorizontalSpace = true;
		data.grabExcessVerticalSpace = false;
		mainComposite.setLayoutData(data);

		GridLayout layout = new GridLayout();
		mainComposite.setLayout(layout);
		setBuildRoot(mainComposite);

		setEnviornment(mainComposite);
		setCommand(mainComposite);
		setCompleteCommand(mainComposite);
		
		return mainComposite;
	}

	private void setBuildRoot(Composite parent) {
		Composite rootComposite = new Composite(parent, SWT.NULL);

		GridLayout rootD = new GridLayout();
		rootD.numColumns = 2;
		rootComposite.setLayout(rootD);
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		rootComposite.setLayoutData(data);

		Label rootDir = new Label(rootComposite, SWT.LEFT);
		rootDir.setText("Root :");

		rootPath = new Text(rootComposite, SWT.SINGLE | SWT.BORDER);
		data = new GridData(GridData.FILL_HORIZONTAL);
		data.grabExcessHorizontalSpace = true;
		rootPath.setLayoutData(data);
	}

	private void setEnviornment(Composite parent) {

		Label enviornmentVar = new Label(parent, SWT.CENTER);
		enviornmentVar.setText("Set enviornment variables");

		enviornmentTxt = new Text(parent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);

		GridData data = new GridData(GridData.FILL_BOTH);
		enviornmentTxt.setLayoutData(data);
	}

	private void setCommand(Composite parent) {

		Label enviornmentVar = new Label(parent, SWT.CENTER | SWT.BORDER_SOLID);
		enviornmentVar.setText("Set Commands");
		commandTxt = new Text(parent, SWT.SINGLE | SWT.BORDER);
		GridData hgd = new GridData(GridData.FILL_HORIZONTAL);
		hgd.grabExcessHorizontalSpace = true;
		commandTxt.setLayoutData(hgd);
	}

	private void setCompleteCommand(Composite parent) {
		Label buildCmdVar = new Label(parent, SWT.CENTER);
		buildCmdVar.setText("Build commands");
		buildCmdTxt = new Text(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL |SWT.BORDER );
		GridData data = new GridData(GridData.FILL_BOTH);
		buildCmdTxt.setLayoutData(data);
	}
	
	private void setValues() {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				rootPath.setText(rootDir);
				enviornmentTxt.setText(enviornmentVar);
				commandTxt.setText(commandVar);
				buildCmdTxt.setText(buildCmdVar);						
			}
       });		
	}

}
